#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/6/15 10:34
# @Author : 闫峰
# @File    : rebatesAndPromotions.py
import requests
from retrying import retry

from third_services.allegro.RebatyesAndPromotions.baseutils import get_token

from utils.otherutils.json_format import json_format
from utils.times_tool.time_control import count_time


class RebatesAndPromotions:

    @retry(stop_max_attempt_number=300, wait_fixed=1000)
    @count_time
    def get_sale_loyalty_promotions(self, ordersourceid, offset, promotionType,limit):
        # url = f"https://api.allegro.pl/sale/loyalty/promotions?limit=50&offset=0&promotionType=MULTIPACK&offer.id={offerid}"
        # promotionType Cross-offer multipack  Multipack
        url = f"https://api.allegro.pl/sale/loyalty/promotions?limit={limit}&offset={offset}&promotionType={promotionType}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        try:
            res = requests.get(url=url, headers=headers,timeout=5)
        except:
            print(
                f"ordersourceid={ordersourceid},/sale/loyalty/promotions?limit=5000&offset={offset}&promotionType={promotionType}请求失败")
            raise
        if res.status_code == 200:
            # return json_format(res)
            return res.json()
        else:
            print(f"ordersourceid={ordersourceid},/sale/loyalty/promotions?limit=5000&offset={offset}&promotionType={promotionType}请求失败")
            print(f"res={res}")
            return Exception("woshiyanfneg")

    @retry(stop_max_attempt_number=3, wait_fixed=1000)
    def get_sale_loyalty_promotions_offerid(self, ordersourceid, offerid):
        # url = f"https://api.allegro.pl/sale/loyalty/promotions?limit=50&offset=0&promotionType=MULTIPACK&offer.id={offerid}"
        url = f"https://api.allegro.pl/sale/loyalty/promotions?offer.id={offerid}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        res = requests.get(url=url, headers=headers)
        if res.status_code==200:
            res = res.json()
        return json_format(res)
        # return res

    @retry(stop_max_attempt_number=3, wait_fixed=1000)
    def get_sale_loyalty_promotions_data(self, ordersourceid, promotionId):
        url = f"https://api.allegro.pl/sale/loyalty/promotions/{promotionId}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        res = requests.get(url=url, headers=headers).json()
        return json_format(res)
        # return res

    @retry(stop_max_attempt_number=5)
    def get_tax(self, ordersourceid, category):
        url = f"https://api.allegro.pl/sale/tax-settings?category.id={category}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        res = requests.get(url=url, headers=headers)
        if res.status_code == 200:
            return res.json()
            # return json_format(res)
        else:
            raise
        # return res

    # 删除活动
    def delete_promotion(self,ordersourceid,promotionid):
        url = f"https://api.allegro.pl/sale/loyalty/promotions/{promotionid}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        res = requests.delete(url=url, headers=headers)
        if res.status_code == 200:
            return res.json()
            # return json_format(res)
        else:
            raise
        # return res

    # 创建促销
    def create_promotion(self,ordersourceid,offer_id):
        url = f"https://api.allegro.pl/sale/loyalty/promotions"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(ordersourceid),
            "Accept-Language": "en-US"
        }
        data = {
    "benefits": [
        {
            "specification": {
                "configuration": {
                    "percentage": "45"
                },
                "trigger": {
                    "discountedNumber": "1",
                    "forEachQuantity": "4"
                },
                "type": "UNIT_PERCENTAGE_DISCOUNT"
            }
        }
    ],
    "offerCriteria": [
        {
            "offers": [
                {
                    "id": offer_id
                }
            ],
            "type": "CONTAINS_OFFERS"
        }
    ]
}
        res = requests.post(url=url, headers=headers,data=data)
        if res.status_code == 200:
            return res.json()
            # return json_format(res)
        else:
            raise

